home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: modifying a 'const' variable, a bug in VC++?
- Date: 25 Feb 1996 21:58:32 GMT
- Organization: self-employed
- Message-ID: <4gqm28$ci3@news.bridge.net>
- References: <00001a81+0000a7f1@msn.com>
- NNTP-Posting-Host: ppp-mia1-43.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
-
- >>>>>>>
- const int i = 1;
- int* j = (int *)&i; // cast a "const int*" to "int*". The
- // compiler should allow it
-
- *j = 2;
- cout << *j << newl; // should print "2"
- cout << i << newl; // should print "2" too, prints 1
-
-
- I consider this a bug in VC++(The correct result should be 2\n2\n.
- Any comments?
-
- <<<<<<<
-
-
- You can't complain. Casting away constancy, as you did, is not valid
- unless the original object referred to is really not const. Your variable
- "i" is const and so the compiler is free to assume that it will never
- change under any circumstances.
-
-
- David
-
-
-